<template>
  <div class="report_wrap">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/home/reports' }">
        数据统计
      </el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- echarts图表区 -->
      <div id="main" style="width: 800px; height: 400px"></div>
    </el-card>
  </div>
</template>

<script>
// 引入echarts
// import echarts from 'echarts'
// 新版需要如下引入
import * as echarts from 'echarts'

import _ from 'lodash'
export default {
  name: 'Report',
  data() {
    return {
      // 需要合并的对象
      options: {
        title: {
          text: '用户来源',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3',
            },
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            boundaryGap: false,
          },
        ],
        yAxis: [
          {
            type: 'value',
          },
        ],
      },
    }
  },
  // 需要在vue实例挂载再进行dom操作
  async mounted() {
    const { data: res } = await this.$http.get('reports/type/1')
    // console.log(res)
    if (res.meta.status !== 200) return this.$msg.error('获取折线图数据失败')

    // 初始化echarts实例并传入dom元素作为容器
    const myMcharts = echarts.init(document.getElementById('main'))
    // 准备数据     merge()合并两个对象
    const result = _.merge(this.options,res.data)
    // 显示图表
    myMcharts.setOption(result)
  },
}
</script>

<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
</style>